<%

// Include file for authentication

function authenticateUser(_NTName)
{
	var NTName = new String(_NTName);
	var objXML = Application("ClientData"); // load cached client data
	var docElement = objXML.documentElement;
	var clientNode = docElement.selectSingleNode("//client[@NTName=\"" + NTName.toUpperCase() + "\"]");
	
	if(clientNode == null)
	{
		DisplayException(1, new Array(_NTName));
	}
	else
	{
		// fill session user_xxx variables with the attributes of this user
		for(i=0; i<clientNode.attributes.length; i++)
		{
			Session("user_" + clientNode.attributes[i].name) = clientNode.attributes[i].value;
			//Out("user_" + clientNode.attributes[i].name + ") = " + clientNode.attributes[i].value + "<br>");
		}
	}
	
	objXML = null;
	return true;
}

function getRemoteUserName()
{
	return String(Session("user_NTName"));
}

function getAllUsers()
{
	var objXML = Application("ClientData"); // load cached client data
	var docElement = objXML.documentElement;
	var users = docElement.selectNodes("client");
	var arr = new Array();
	for(var i=0;i<users.length;i++) arr.push(users[i].attributes[1].value);
	return arr;
}


function checkUserShift()
{
	var shiftStart = new String(Session("user_shiftStart"));
	var shiftEnd = new String(Session("user_shiftEnd"));
	var dateNow = new Date();
	var shiftStartHour = new String(shiftStart.substr(0, 2));
	var nowH = new String(dateNow.getHours());
	if(nowH.length==1)
	{
		nowH = "0" + nowH;
	}
	nowM = new String(dateNow.getMinutes());
	if(nowM.length==1)
	{
		nowM = "0" + nowM;
	}
	var now = new String(nowH + ":" + nowM);
	var shiftStartCalculated = getAsMinutes(shiftStart);
	var shiftEndCalculated = getAsMinutes(shiftEnd);
	var nowCalculated = getAsMinutes(now);
	if(nowCalculated < shiftStartCalculated || nowCalculated > shiftEndCalculated)
	{
		DisplayException(2, new Array(shiftStart, shiftEnd));
	}
}

function getAsMinutes(_time) // send as hh:mm
{
	var hh = new String(_time.substr(0, 2));
	var mm = new String(_time.substr(3, 2));
	if(hh.substr(0, 1) == "0") hh = hh.substr(1, 1);
	if(mm.substr(0, 1) == "0") mm = mm.substr(1, 1);
	return parseInt(hh) * 60 + parseInt(mm);
}

function checkAuthentication()
{
	NTName = new String(Session("user_NTName"));
	if(NTName.length == 0)
	{
		authenticateUser(); // displays exception if note authenticated
		return true;
	}
	else
	{
		return true;
	}
}

function checkPassword(_NTName, _password)
{
	var NTName = new String(_NTName);
	var password = new String(_password);
	var objXML = Application("ClientData"); // load cached client data
	var docElement = objXML.documentElement;
	var clientNode = docElement.selectSingleNode("//client[@NTName=\"" + NTName.toUpperCase() + "\"]");
	
	objXML = null;

	if(clientNode == null)
	{
		return false;
	}
	else
	{
		return clientNode.attributes[2].value == password;
	}
	

}

function getUserDisplayName(_NTName)
{
	var NTName = new String(_NTName);
	var objXML = Application("ClientData"); // load cached client data
	var docElement = objXML.documentElement;
	var clientNode = docElement.selectSingleNode("//client[@NTName=\"" + NTName.toUpperCase() + "\"]");
	if(clientNode)
	{
		var retName = new String(clientNode.attributes[0].value);
	}
	else
	{
		retName = _NTName;
	}
	clientNode = null;
	objXML = null;
	return retName;
}


function displayLoginScreen()
{
%>
	<div align="center">
	
	<br>
	<form action="?screenType=login" method="post">
	<table align="center" width="253" height="158" cellpadding="0" cellspacing="0" border="0">	
	<tr>
		<td width="190">
			<img src="/oxWebCATI/images/app/lock.jpg" border="0">
		</td>
		<td width="*">
			<table align="center" width="253" height="158" cellpadding="0" cellspacing="0" border="0" background="/oxWebCATI/images/app/login.gif">
				<tr>
					<td width="134" height="41">&nbsp;</td>
					<td width="94">&nbsp;</td>
					<td width="25">&nbsp;</td>
				</tr>
				<tr>
					<td width="134" height="20">&nbsp;</td>
					<td width="94"><input type="text" class="loginBox" name="uName"></td>
					<td width="25">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" height="12">&nbsp;</td>
				</tr>
				<tr>
					<td width="134" height="20">&nbsp;</td>
					<td width="94"><input type="password" class="loginBox" name="uPass"></td>
					<td width="25">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" height="58" align="right"><input type="image" name="loginSubmit" src="/oxWebCATI/images/app/loginButton.gif" border="0">&nbsp;&nbsp;&nbsp;</td>
				</tr>
			</table>
		</td>
	</tr>
	</table>
	</form>
	</div>
<%
}

%>